package com.LostAndFound.service;

import java.sql.SQLException;
import java.util.List;

import com.LostAndFound.dao.CategoryListDao;
import com.LostAndFound.domain.Category;
import com.LostAndFound.utils.DataSourceUtils;

public class CategoryListService {

	CategoryListDao dao = new CategoryListDao();
	
	public List<Category> showAllCategoryName() {
		List<Category> categoryList = null; 
		try {
			categoryList = dao.showAllCategoryName();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return categoryList;
	}

	public int getTotalCategoryListForEasyUI() {
		int total = 0;
		try {
			total = dao.getTotalCategoryListForEasyUI();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}
	
	public List<Category> getCategoryListForEasyUI(int pageNumber, int pageSize) {
		List<Category> categoryList = null;
		int startIndex = (pageNumber-1)*pageSize;
		try {
			categoryList = dao.getCategoryListForEasyUI(startIndex,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return categoryList;
	}
	// 开启事务，删除category，并将相应的article中的cid置为“其他”
	public boolean handleDeleteCategoryForEasyUI(String cid) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.alterArticleCidByCid(cid);
			dao.deleteCategoryByCid(cid);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}
	// 编辑分类
	public boolean handleEditCategoryEasyUI(Category category) {
		boolean success = false;
		try {
			dao.handleEditCategoryEasyUI(category);
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return success;
	}
	public boolean handleAddCategoryEasyUI(Category category) {
		boolean success = false;
		try {
			dao.handleAddCategoryEasyUI(category);
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return success;
	}

}
